import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import vueRouter from "@/router";
import {Notification} from 'element-ui'

// Vue.prototype.$notify = Notification;
Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        login: false,
        showLoginWrapper: false,
        toWhere: "",
        user: {
            userId: 0,
            userName: "NotLogin",
            email: "",
            permission: ""
        },
        form: {
            searchType: "expert",
            pageIndex: 0,
            pageSize: 30,
            keyWord: "",
            region: ["全部"],
            domain: ["全部"],
            honor: ["全部"],
            meeting: ["全部"]
        },
        dbtotal: {
            expert: 0,
            team: 0
        },
        searchResults: {
            data: []
        },
        favoritesResults: {
            data: []
        },
        preselectResults: {
            data: []
        },
        currentMenu: {
            label: "首页",
            path: "Index"
        },
        tabsList: [{
            label: "首页",
            path: "Index"
        }]
    },
    mutations: {
        me(state) {
            0 === state.user.userId && axios.get("api/user/me").then(res => {
                    if (200 === res.data.code) {
                        state.user = res.data.data
                        state.login = true
                        console.log("me", state.user)
                    } else {
                        if (!state.user.email) {
                            vueRouter.push("/UserCenter/Setting")
                            Notification({
                                title: "邮箱未激活",
                                message: "请激活您的邮箱！",
                                type: "info",
                                offset: 200,
                                duration: 0
                            })
                        }
                    }
                }
            )
        },
        showLoginWrapper(state, e) {
            state.showLoginWrapper = false !== e
        },
        doLogin(state) {
            state.showLoginWrapper = false
            this.commit("me", state)
            "" !== state.toWhere && vueRouter.push(state.toWhere)
        },
        doLogout(state) {
            state.login = false
            axios.get("api/user/logout").then(e => {
                    200 === e.data.code && (state.login = false,
                        state.user = {
                            userId: 0,
                            userName: "NotLogin",
                            email: "",
                            permission: ""
                        },
                        state.searchResults = {
                            data: []
                        },
                        state.favoritesResults = {
                            data: []
                        },
                        state.preselectResults = {
                            data: []
                        },
                        state.form = {
                            searchType: "expert",
                            pageIndex: 0,
                            pageSize: 30,
                            keyWord: "",
                            region: ["全部"],
                            domain: ["全部"],
                            honor: ["全部"],
                            meeting: ["全部"]
                        })
                }
            )
        },
        search(state, e) {
            state.dbtotal.expert || axios.get("api/expert/count").then(e => {
                    200 === e.data.code && (state.dbtotal.expert = e.data.data)
                }
            )
            state.dbtotal.team || axios.get("api/team/count").then(e => {
                    200 === e.data.code && (state.dbtotal.team = e.data.data)
                }
            )
            state.searchResults.data = []
            e ? state.form = e : state.form.pageIndex = 0
            axios.post("api/" + e.searchType + "/search", state.form).then(e => {
                    200 === e.data.code && (state.searchResults = e.data)
                }
            ).catch(err => console.log(err))
        },
        pageIndexChange(state, currentPageIndex) {
            state.form.pageIndex = currentPageIndex
            this.commit("search", state.form)
        },
        getAllFavorites(state, type) {
            state.favoritesResults.data = [];
            let url = "";
            type = type.toLowerCase(),
                "expert" === type ? url = "api/expert/getAllFavorites" : "team" === type && (url = "api/team/getAllFavorites"),
                axios.get(url).then(res => {
                        200 === res.data.code && (state.favoritesResults = res.data)
                    }
                ).catch(err => console.log(err))
        },
        addFavorite(state, payload) {
            let [type, item] = payload
            type = type.toLowerCase();
            let url = ""
            let msg = "";
            if ("expert" === type) {
                url = "api/expert/addFavorite?expertId=" + item.expertId
                msg = item.expertName
            } else if ("team" === type) {
                url = "api/team/addFavorite?teamId=" + item.teamId
                msg = item.teamChName
            }
            axios.get(url).then(res => {
                    if (200 === res.data.code) {
                        Notification({
                            title: "收藏",
                            message: "成功添加收藏(" + msg + ")",
                            type: "success",
                            offset: 200
                        })
                        item.favorite = true
                        item.favorsNum = res.data.data
                    }
                }
            ).catch(err => console.log(err))
        },
        removeFavorite(state, payload) {
            let [type, item] = payload
            type = type.toLowerCase();
            let url = ""
            let msg = "";
            if ("expert" === type) {
                url = "api/expert/removeFavorite?expertId=" + item.expertId
                msg = item.expertName
            } else if ("team" === type) {
                url = "api/team/removeFavorite?teamId=" + item.teamId
                msg = item.teamChName
            }
            axios.get(url).then(res => {
                    if (200 === res.data.code) {
                        Notification({
                            title: "取消收藏",
                            message: "成功取消收藏(" + msg + ")",
                            type: "success",
                            offset: 200
                        })
                        item.favorite = false
                        item.favorsNum = res.data.data
                        if ("expert" === type) {
                            state.favoritesResults.data = state.favoritesResults.data.filter((state => {
                                    return state.expertId !== item.expertId
                                }
                            ))
                        } else if ("team" === type) {
                            state.favoritesResults.data = state.favoritesResults.data.filter((state => {
                                    return state.teamId !== item.teamId
                                }
                            ))
                        }
                    }
                }
            ).catch(err => console.log(err))
        },
        getAllPreselects(state, e) {
            state.preselectResults.data = [];
            let a = "";
            e = e.toLowerCase(),
                "expert" === e ? a = "api/expert/getAllPreselects" : "team" === e && (a = "api/team/getAllPreselects"),
                axios.get(a).then(res => {
                        200 === res.data.code && (state.preselectResults = res.data)
                    }
                ).catch(err => console.log(err))
        },
        addPreselect(state, payload) {
            let [type, item] = payload
            type = type.toLowerCase();
            let url = ""
            let msg = "";
            if ("expert" === type) {
                url = "api/expert/addPreselect?expertId=" + item.expertId
                msg = item.expertName
            } else if ("team" === type) {
                url = "api/team/addPreselect?teamId=" + item.teamId
                msg = item.teamChName
            }
            axios.get(url).then(res => {
                    if (200 === res.data.code) {
                        Notification({
                            title: "预选",
                            message: "成功添加预选(" + msg + ")",
                            type: "success",
                            offset: 200
                        })
                        item.preselect = true
                    }
                }
            ).catch(err => console.log(err))
        },
        removePreselect(state, payload) {
            let [type, item] = payload
            type = type.toLowerCase();
            let url = ""
            let msg = "";
            if ("expert" === type) {
                url = "api/expert/removePreselect?expertId=" + item.expertId
                msg = item.expertName
            } else if ("team" === type) {
                url = "api/team/removePreselect?teamId=" + item.teamId
                msg = item.teamChName
            }
            axios.get(url).then(res => {
                    if (200 === res.data.code) {
                        Notification({
                            title: "取消预选",
                            message: "成功取消预选(" + msg + ")",
                            type: "success",
                            offset: 200
                        })
                        item.preselect = false
                        if ("expert" === type) {
                            state.preselectResults.data = state.preselectResults.data.filter((state => {
                                    return state.expertId !== item.expertId
                                }
                            ))
                        } else if ("team" === type) {
                            state.preselectResults.data = state.preselectResults.data.filter((state => {
                                    return state.teamId !== item.teamId
                                }
                            ))
                        }
                    }
                }
            ).catch(err => console.log(err))
        },
        clearPreselects(state, e) {
            e = e.toLowerCase();
            let a = "";
            "expert" === e ? a = "api/expert/clearPreselects" : "team" === e && (a = "api/team/clearPreselects"),
                axios.get(a).then(e => {
                        if (200 === e.data.code) {
                            if (Notification({
                                title: "预选",
                                message: "成功清空预选",
                                type: "success",
                                offset: 200
                            }), state.searchResults.data)
                                for (let a = state.searchResults.data.length - 1; a > -1; a--)
                                    state.searchResults.data[a].preselect = false;
                            if (state.favoritesResults.data)
                                for (let n = state.favoritesResults.data.length - 1; n > -1; n--)
                                    state.favoritesResults.data[n].preselect = false;
                            state.preselectResults.data = []
                        }
                    }
                ).catch(err => console.log(err))
        },
        //拖拽排序
        sort(state, payload) {
            let type = payload[0]
            let dataSetType = payload[1]
            let id_list = payload[2]
            let url = "api/" + type.toLowerCase() + "/add" + dataSetType + "s";
            axios.post(url, id_list).then(res => {
                    if (200 === res.data.code) {
                        Notification({
                            title: "保存成功",
                            message: "排序已成功保存！",
                            type: "success",
                            offset: 200
                        })
                    }
                }
            ).catch(err => console.log(err))
        },
        selectMenu(state, e) {
            if ("首页" !== e.name) {
                state.currentMenu = e;
                const a = state.tabsList.findIndex((function (state) {
                        return state.label === e.label
                    }
                ));
                -1 === a && state.tabsList.push(e)
            } else
                state.currentMenu = {
                    label: "首页",
                    path: "Index"
                }
        },
        closeTab(state, e) {
            const a = state.tabsList.findIndex((function (state) {
                    return state.label === e.label
                }
            ));
            state.tabsList.splice(a, 1)
        }
    },
    actions: {},
    getters: {},
    modules: {}
})

export default store
